<?php

  if(!isset($_GET['group_id']))
  {
	header('Location:index.php?module=group_manager');
  }
  else
  {

	$showPage = false;
	if($authority->getAuthority('edit_group',$_SESSION['id']) == true)
	{

		if($_POST)
		{
			if($db->sql(" UPDATE snm_group SET group_name = '".$db->escape($_POST['group_name'])."', note = '".$db->escape($_POST['note'])."' WHERE id=".intval($_GET['group_id'])))
			{
				$hata['sonuc'] = true;
				$hata['aciklama'] = ' Group Name and Note Edited.';
				if($db->sql("DELETE FROM snm_authority WHERE group_id=".intval($_GET['group_id'])))
				{
					$hata['aciklama'] .= ' <br> Old Authority Deleted. New Authority Add.';
					foreach($_POST['auth'] as $modul=>$value)
					{
						if(!$db->sql(" INSERT INTO snm_authority (group_id , module) VALUES (".intval($_GET['group_id']).", '".$modul."' ) "))
						{
							$hata['aciklama'] .= '<b style="color:red;"> Error:</b>'.$modul.' Cant Added! <br />';
						}
					}					
				}
				else
				{
					$hata['aciklama'] .= '<b style="color:red;">ERROR!</b> Old Authority Not Deleted! Please Contact Administrator.';
				}
			}
			else
			{
				$hata['sonuc'] = false;
				$hata['aciklama'] = ' SQL ERROR!'; 
			}
		}
		
		
		$showPage = true;
		$group = $authority->getGroup($_GET['group_id'])[0];
		$groupAuthority = $authority->getAllAuthority($_GET['group_id']);
	}
	else
	{

		$hata['sonuc'] = false;
		$hata['aciklama'] = ' <strong>Error!</strong> you cant login this page!.';
	}
  }
  

  function checkAuth($module,$groupAuthority)
  {
	$return = false;
	foreach($groupAuthority as $group)
	{
		if($module == $group['module'])
		{
			$return = true;
		}
	}
	
	return $return;
  }
  
  include "template/edit_group.php";
?>